package com.bocloud.cmp.driver.providers.jd;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.cmp.driver.providers.LbDriver;
import com.bocloud.cmp.model.LbModel;
import com.bocloud.common.model.BsmResult;
import com.jcloud.cap.client.ClientConfig;
import com.jcloud.cap.client.IClient;
import com.jcloud.cap.client.JCloudClient;
import com.jcloud.cap.client.builder.v1.CreateLoadbalanceRequestBuilder;
import com.jcloud.cap.client.builder.v1.DeleteLoadbalanceRequestBuilder;
import com.jcloud.cap.client.builder.v1.QueryLoadbalanceDetailRequestBuilder;
import com.jcloud.cap.client.builder.v1.QueryLoadbalancesRequestBuilder;
import com.jcloud.cap.client.exception.ClientException;
import com.jcloud.cap.client.exception.ServerException;
import com.jcloud.cap.client.http.HttpConfig;
import com.jcloud.cap.data.model.v1.CreateLoadbalanceResponse;
import com.jcloud.cap.data.model.v1.DeleteLoadbalanceResponse;
import com.jcloud.cap.data.model.v1.QueryLoadbalanceDetailResponse;
import com.jcloud.cap.data.model.v1.QueryLoadbalancesResponse;

public class JdLbDriver extends JdDriver implements LbDriver {
	private static Logger logger = LoggerFactory.getLogger(JdLbDriver.class);
	private IClient client;

	/**
	 * @param accessKey
	 * @param secretKey
	 * @param endpoint
	 */
	public JdLbDriver(String accessKey, String secretKey, String endpoint, String region) {
		super(accessKey, secretKey, endpoint, region);
		ClientConfig config = new ClientConfig();
		HttpConfig httpConfig = new HttpConfig();
		// 调用地址
		httpConfig.setEndpoint(endpoint);
		httpConfig.setReadTimeout(20000);
		httpConfig.setConnectTimeout(20000);
		config.setHttpConfig(httpConfig);
		config.setCredential(this.getCredential().getCapCredential());
		client = new JCloudClient(config);
	}

	@Override
	public BsmResult list() {
		QueryLoadbalancesRequestBuilder builder = QueryLoadbalancesRequestBuilder.create();
		this.init(builder, "network", "queryLoadbalances", this.getRegion());
		try {
			QueryLoadbalancesResponse response = client.send(builder.build());
			return new BsmResult(true, response.getLoadbalances(), "success");
		} catch (ClientException | ServerException e) {
			logger.error("List volume [{}] exception!", e);
			return new BsmResult(false, "List volume failed");
		}
	}

	@Override
	public BsmResult create(LbModel lb) {
		CreateLoadbalanceRequestBuilder builder = CreateLoadbalanceRequestBuilder.create();
		this.init(builder, "network", "createLoadbalance", this.getRegion());
		try {
			CreateLoadbalanceResponse response = client.send(builder.poolName(lb.getName()).subnetId(lb.getSubnetId())
					.description(lb.getDescription()).listenProtocol(lb.getListenProtocol()).lbMethod(lb.getLbMethod())
					.port(lb.getPort()).protocol(lb.getProtocol()).feeType(1).build());
			return new BsmResult(true, response.getRequestId(), "success");
		} catch (ClientException | ServerException e) {
			logger.error("Create loadbalance [{}] exception!", e);
			return new BsmResult(false, "Create loadbalance failed");
		}

	}

	@Override
	public BsmResult remove(String lbId) {
		DeleteLoadbalanceRequestBuilder builder = DeleteLoadbalanceRequestBuilder.create();
		this.init(builder, "network", "deleteLoadbalance ", this.getRegion());
		try {
			DeleteLoadbalanceResponse response = client.send(builder.poolId(lbId).build());
			return new BsmResult(true, response.getRequestId(), "success");
		} catch (ClientException | ServerException e) {
			logger.error("Reomve loadbalance [{}] exception!", e);
			return new BsmResult(false, "Reomve loadbalance failed");
		}
	}

	@Override
	public BsmResult detail(String lbId) {
		QueryLoadbalanceDetailRequestBuilder builder = QueryLoadbalanceDetailRequestBuilder.create();
		this.init(builder, "network", "queryLoadbalanceDetail", this.getRegion());
		try {
			QueryLoadbalanceDetailResponse response = client.send(builder.poolId(lbId).build());
			return new BsmResult(true, response.getLoadbalance(), "success");
		} catch (ClientException | ServerException e) {
			logger.error("Query loadbalance [{}] exception!", e);
			return new BsmResult(false, "Query loadbalance failed");
		}
	}

}
